<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入订单号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable style="width: 150px">
          <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- 强制换行 -->
      <el-form-item style="width: 100%;"></el-form-item>
      <el-form-item label="收货人姓名" prop="receiverName">
        <el-input
            v-model="queryParams.receiverName"
            placeholder="请输入收货人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="receiverPhone">
        <el-input
            v-model="queryParams.receiverPhone"
            placeholder="请输入收货人电话"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['order:orderInfo:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单号" align="center" prop="orderNo" width="300"/>
      <el-table-column label="订单总额" align="center" prop="totalAmount">
        <template #default="{ row }">
          {{ row.totalAmount }} 元
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="收货人姓名" align="center" prop="receiverName"/>
      <el-table-column label="收货人电话" align="center" prop="receiverPhone" width="150"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleShow(scope.row)"
                     v-hasPermi="['order:orderInfo:list']">订单详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 订单详情对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" append-to-body>
      <el-form ref="orderInfoRef" :model="form" :rules="rules" label-width="120px">
        <el-divider/>
        <span style="margin-bottom: 5px;font-weight:bold;">订单基本信息</span>
        <el-divider/>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单号：">
              {{ form.orderNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单总额：">
              {{ form.totalAmount + ' 元' }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="运费：">
              {{ form.feightFee }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式：">
              {{form.paymentTime ? '支付宝' : '未支付'}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单状态：">
              {{
                form.orderStatus === 0 ? '待支付' : form.orderStatus === 1 ? '待发货' : form.orderStatus === 2 ? '已发货' :
                    form.orderStatus === -1 ? '已取消' : '完成'
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间：">
              {{ form.createTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支付时间：">
              {{ form.paymentTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单备注：">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider/>
        <span style="margin-bottom: 5px;font-weight:bold;">收货人信息</span>
        <el-divider/>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货人姓名：">
              {{ form.receiverName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货人电话：">
              {{ form.receiverPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货省市：">
              {{ form.receiverProvince + " " + form.receiverCity }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货区/县：">
              {{ form.receiverDistrict }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址标签：">
              {{ form.receiverTagName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货详细地址：">
              {{ form.receiverAddress }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider/>
        <span style="margin-bottom: 5px;font-weight:bold;">订单明细信息</span>
        <el-divider/>
        <el-table :data="form.orderItemList" style="width: 100%">
        <el-table-column prop="skuName" label="商品SKU名称"  />
        <el-table-column label="商品图片" #default="scope" width="150">
          <img :src="scope.row.thumbImg" width="50" />
        </el-table-column>
        <el-table-column prop="skuPrice" label="SKU价格" width="120"/>
        <el-table-column prop="skuNum" label="购买数量" width="120"/>
      </el-table>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderInfo">
import {listOrderInfo, getInfo} from "@/api/order/orderInfo";

const {proxy} = getCurrentInstance();
const {order_status} = proxy.useDict('order_status');
const orderStatusList = ref([
  { id: 0, name: '待支付' },
  { id: 1, name: '待发货' },
  { id: 2, name: '已发货' },
  { id: 3, name: '完成' },
  { id: -1, name: '已取消' },
])


const orderInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {
    orderNo: null,
    totalAmount: null,
    feightFee: null,
    orderStatus: null,
    paymentTime: null,
    remark: null,
    receiverName: null, // 收货人姓名
    receiverPhone: null, // 收货人电话
    receiverTagName: null, // 地址标签
    receiverProvince: null, // 省
    receiverCity: null, // 市
    receiverDistrict: null, // 区/县
    receiverAddress: null, // 收货详细地址
    orderItemList: [
      {
        skuName: null,
        thumbImg: null,
        skuPrice: null,
        skuNum: null
      }
    ]
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    orderStatus: null,
    receiverName: null,
    receiverPhone: null,
    createTime: null,
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询订单列表列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listOrderInfo(queryParams.value).then(response => {
    orderInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleShow(row) {
  const id = row.id || ids.value
  getInfo(id).then(response => {
    console.log(response)
    form.value = response.data;
    open.value = true;
    title.value = "订单详情";
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/orderInfo/export', {
    ...queryParams.value
  }, `orderInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
